class Solution {
public:
    int minimizeMax(vector<int>& nums, int p) {
        sort(nums.begin(), nums.end());
        int left = 0;
        int right = 1e9 + 1;
        while (left < right)
        {
            int mid = (left + right) / 2;
            if (check(nums, p, mid))
            {
                right = mid;
            }
            else
            {
                left = mid + 1;
            }
        }
        return left;
    }

    bool check(vector<int>& nums, int p, int mid)
    {
        int count = 0;
        for (int i = 0; i < nums.size() - 1; i++)
        {
            if (nums[i + 1] - nums[i] <= mid)
            {
                count++;
                i = i + 1;
            }
        }
        return count >= p;
    }


};